package com.robot.log.listener;

import com.robot.log.pojo.SearchLog;
import com.robot.log.service.SearchLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author wuyunbin
 */
@Component
@RabbitListener(queues = "logs")
public class LogListener {

    @Autowired
    SearchLogService searchLogService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RabbitHandler
    public void execute(HashMap<String, Object> params) {
        logger.info(params.toString());
        String type = (String) params.get("type");
        String search = "search";
        if (search.equals(type)) {

            String keywords=(String)params.get("keywords");

            if(keywords!=null){
                keywords=keywords.trim();

                SearchLog searchLog=new SearchLog();
                searchLog.setKeywords(keywords);
                searchLog.setMemberId((String)params.get("memberId"));

                String province = (String) params.get("province");
                if (province != null && !"".equals(province)) {
                    params.put("province", province);
                    searchLog.setProvince(province);
                }
                String provinceId = (String) params.get("provinceId");
                if (provinceId != null && !"".equals(provinceId)) {
                    params.put("provinceId", provinceId);
                    searchLog.setProvinceId(provinceId);
                }
                String city = (String) params.get("city");
                if (city != null && !"".equals(city)) {
                    params.put("city", city);
                    searchLog.setCity(city);
                }
                String cityId = (String) params.get("cityId");
                if (cityId != null && !"".equals(cityId)) {
                    params.put("cityId", cityId);
                    searchLog.setCityId(cityId);
                }
                String area = (String) params.get("area");
                if (area != null && !"".equals(area)) {
                    params.put("area", area);
                    searchLog.setArea(area);
                }
                String areaId = (String) params.get("areaId");
                if (areaId != null && !"".equals(areaId)) {
                    params.put("areaId", areaId);
                    searchLog.setAreaId(areaId);
                }

                searchLogService.add(searchLog);
            }
        }
    }
}
